﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace OF.Controls
{
    /// <summary>
    /// 文件名称：排序控件
    /// </summary>
    [Serializable]
    [DefaultEvent("Click")]
    [DefaultProperty("Text")]
    [ToolboxData(@"<{0}:SortLink runat=server>SortLink</{0}:SortLink>")]
    public class SortLink : System.Web.UI.WebControls.LinkButton, ISort
    {
        public SortLink()
        {
        }

        protected SortLink(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");
            SortField = (string)info.GetValue("SortField", typeof(string));
            IsAsc = (bool)info.GetValue("IsAsc", typeof(bool));
            SortGroupName = (string)info.GetValue("SortGroupName", typeof(string));
        }

        /// <summary>
        /// 获取或设置sql排序字段
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), Description("获取或设置sql排序字段。")]
        public string SortField
        {
            get
            {
                object o = ViewState["SortField"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["SortField"] = value.Trim();
            }
        }

        /// <summary>
        /// 获取或设置sql排序是否升序
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), Description("获取或设置sql排序是否升序。")]
        public bool IsAsc
        {
            get
            {
                var o = ViewState["IsAsc"];
                return (o == null) || Convert.ToBoolean(o);
            }
            set
            {
                ViewState["IsAsc"] = value;
            }
        }
        /// <summary>
        /// 获取sql排序语句
        /// </summary>
        /// <returns></returns>
        public string GetSqlSort()
        {
            return SortField + " " + (IsAsc ? "asc" : "desc") + " ";
        }

        protected override void OnClick(EventArgs e)
        {
            IsAsc = !IsAsc;
            base.OnClick(e);
        }

        /// <summary>
        /// 获取或设置sql排序分组
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), Description("获取或设置sql排序分组。")]
        public string SortGroupName
        {
            get
            {
                object o = ViewState["SortGroupName"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["SortGroupName"] = value.Trim();
            }
        }

        /// <summary>
        /// 序列化进程
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");
            info.AddValue("SortField", SortField);
            info.AddValue("IsAsc", IsAsc);
            info.AddValue("SortGroupName", SortGroupName);
            info.AddValue("Text", Text);
        }
    }
}
